﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Invoicing.Web.Global;

namespace Invoicing.Web.Management
{
    
    public class Common
    {
        public static log4net.ILog Logger
        {
            get
            {
                return log4net.LogManager.GetLogger("PHLogger");
            }
        }
        /// <summary>
        /// 登陆时用的验证码
        /// </summary>
        //public static string CheckCode
        //{
        //    get
        //    {
        //        return (string)MicroSchool.Web.WebContext.CurrentContext.Session["LoginCheckCode"];
        //    }
        //    set
        //    {
        //        MicroSchool.Web.WebContext.CurrentContext.Session["LoginCheckCode"] = value;
        //    }
        //}
        /// <summary>
        /// 抛出http异常
        /// </summary>
        /// <param name="code"></param>
        public static void ThrowHttpException(int code)
        {
            throw new HttpException(code, "");
        }
        /// <summary>
        /// 显示单据名称
        /// </summary>
        /// <param name="orderMethod"></param>
        /// <returns></returns>
        public static string GetOrderName(int orderMethod)
        {
            switch (orderMethod)
            {
                case 1:
                    return "采购单";
                case 2:
                    return "销售单";
                case 3:
                    return "采购退货单";
                case 4:
                    return "销售退货单";
                case 11:
                    return "入库单";
                case 12:
                    return "出库单";
                case 13:
                    return "盘存单";
                case 14:
                    return "调拨单";
                case 21:
                    return "付款单";
                case 22:
                    return "收款单";
                case 31:
                    return "采购订单";
                case 32:
                    return "销售订单";
                default:
                    return "";
            }
        }
        public static string GetOrderStatus(int orderStatus)
        {
            switch (orderStatus)
            {
                case 1:
                    return "新单据";
                case 3:
                    return "部分库存";
                case 5:
                    return "仓库完成";
                case 8:
                    return "部分支付";
                case 10:
                    return "全部支付";
                case 21:
                    return "订单进行中";
                case 23:
                    return "订单已完成";
                case 99:
                    return "已撤消";
                default:
                    return "未知";
            }
        }
        /// <summary>
        /// 权限检查
        /// </summary>
        /// <param name="allowRole"></param>
        /// <returns></returns>
        public static bool AuthorizeCheck(string allowRole)
        {
            if (WebContext.CurrentUser.Roles.Any(p => p.RoleName == "Admin"))
                return true;
            if (WebContext.CurrentUser.Roles.Any(p => p.RoleName == allowRole))
                return true;
            return false;
        }
        /// <summary>
        /// 是否显示主菜单
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public static bool ShowMainMenu(string[] roles)
        {
            if (WebContext.CurrentUser.Roles.Any(p => p.RoleName == "Admin"))
                return true;
            if (WebContext.CurrentUser.Roles.Any(p => roles.Contains(p.RoleName)))
                return true;
            return false;
        }
    }
}